<?php
session_start();

if (!isset($_SESSION['username']) and !isset($_SESSION['password'])) {
    header("Location: index.php");
}
?>
<!DOCTYPE html>
<html>
    <head>
        <link rel="stylesheet" type="text/css" href="../css/projectstyle.css">
        <meta charset="UTF-8">
        <title></title>
        <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.6.2/jquery.min.js"></script>

        <script>

            // all jquery ar enfolded in  $(document).ready(function(){ //code here}
            $(document).ready(function()
            {
                
                // fades in hidden form
                $(".btn1").click(function(){
                         $("div").fadeIn();
                 });   
                 
                 $(".btn2").click(function(){
                     // prevent form from being submitted
                     event.preventDefault();
                      $("#addlect").val("");
                         $("#hide").fadeOut();
                 });
                 
                 
            });

        </script>


    </head>
    <body>
        
        
        
        
        <div id= "container" name="container" class = "container">
            <div id="menuwrap">   <?php include("../include/header.php"); ?></div>
            <?php include("../include/sidepanel.php"); ?>
            <div id ="cont">
               
 <?php
                if ($_SERVER["REQUEST_METHOD"] == "POST") {
                    /* --------------------------------------------------------------------- */
                    // mysql connection
                    $user = $_SESSION['user'];
                    $password = $_SESSION['password'];
                    $database = $_SESSION['database'];
                    $host = $_SESSION['host'];

                    $con = mysqli_connect($host, $user, $password, $database);
                    /* --------------------------------------------------------------------- */
                    $courseid = $_POST['courseid'];
                    $coursename = $_POST['coursename'];
                    echo "<p>Dina lektioner i ämnet " . $coursename . " med kurskod " . $courseid . "</p>";

                    $select = "SELECT COURSE_ID , LECTURE_NAME,LECTURE_ID FROM LECTURE WHERE COURSE_ID='$courseid';";

                    if (mysqli_connect_errno()) { //checks connection and output error if connection fails
                        echo "connection failed " . mysqli_connect_error();
                    }

                    $result = mysqli_query($con, $select);
                    ?>
                    <div class="wrap">
                        <table id="tbl">
                            <tr>

                                <th>Lektionsnamn</th> <!--table header which is a cell-->
                                <th colspan="2">Åtgärd</th>		
                                <!--colspan makes cell width size as two-->
                            </tr>
                            <?php
                            // iterates result 
                            while ($row = mysqli_fetch_array($result)) {

                                $lectureName = $row['LECTURE_NAME'];
                                $lectureid = $row['LECTURE_ID'];
                                ?>
                                <tr>
                                    <td><?php echo $lectureName; ?></td>

                                    <td class="rightborder">
                                        <form action="deletelecture.php" method="post" id="id<?php echo $lectureid;?>" name="delete" class="delete">
                                            <!-- hidden field contains values from database used to identify the course to remove-->
                                            <input type="hidden" value="<?php echo $lectureid; ?>" name="lectureid" id="lectureid"></input>
                                            <button class="button">Ta bort</button>
                                        </form>
                                    </td>

                                    <td class="leftborder">
                                        <form action="../lectures/editlectures.php" method="post">
                                            <!-- hidden field contains values from database used to identify the course to remove-->
                                            <input type="hidden" value="<?php echo $lectureid; ?>" name="lectureid" id="lectureid"></input>
                                            <input type="submit" value="Redigera" onclick="return confirm('vill du redigera lektionen ?')"/>
                                        </form>
                                    </td>

                                </tr>
                                <?php
                               
                            }
                        }
                        ?>


                    </table>
                        
                        
                </div>
                
                <button class="btn1">Lägg till lektion</button>
            
            </div>
            <!-- form used to add lecture div not displayed -->
        <div id="hide">
            <form class="frmc" id="addlect" action="../lectures/addlectures.php" method="post">
                <p>Lektionsnamn: <input type="text" name ="lecturename" required></input></p>
                    <!--<input  name = "courseName" required></input></p>-->
                <input type="hidden" name = "courseid" id="<?php echo $courseid;?>" value="<?php echo $courseid; ?>"/>
                <button class="btn2">Avbryt</button>
                <input type="submit" value="spara">
            </form> 
            
        </div>
            
            
            
            
        </div>
            <script>
                 $(document).ready(function()
            {
                // Attach a submit handler to the form
                 $( "#addlect" ).submit(function( event ) {
                     //alert('hello');
                     // prevents form submitt from reloading form
                     event.preventDefault();
                     // Get some values from elements on the page:
                     var $form = $( this ),
                     lect = $form.find( "input[name='lecturename']" ).val(),
                     id   = $form.find( "input[name='courseid']" ).val(),
                     url = $form.attr( "action" );
                     // Send the data using post
                     var posting = $.post( url, { lecturename: lect, courseid:id} ,function(data){ 
                     $('#tbl').append(data);});
                     $("input:text").val("");    
               });
           });
            </script>
            
            
          <script>
               $(document).ready(function()
            {
                // Attach a submit handler to the form
               $( ".delete" ).submit(function( event ) {
 
             // Stop form from submitting normally
              event.preventDefault();
 
            // Get some values from elements on the page:
            var $form = $( this ),
            lect= $form.find( "input[name='lectureid']" ).val(),
            url = $form.attr( "action" );
 
            // Send data using post function data gets any response from php webpage
            var posting = $.post( url, { lectureid: lect} ,function(data){ alert(data);});
  
            $(this).closest('tr').remove();
  
            });
        });           
            
            </script> 
        
        
            
    </body>
</html>
